package org.ww.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import org.ww.edu.bean.Subject;
import org.ww.edu.bean.excel.ExcelSubject;
import org.ww.edu.bean.subject.OneSubject;
import org.ww.edu.bean.subject.TwoSubject;
import org.ww.edu.listener.SubjectExcelListener;
import org.ww.edu.mapper.SubjectMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.ww.edu.service.SubjectService;
import org.ww.servicebase.exceptionhandler.GuliException;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author ww
 * @since 2020-10-20
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    /**
     * 添加课程
     * @param subjectService
     * @param file
     */
    @Override
    public void addSubject(SubjectService subjectService, MultipartFile file) {
        try{
            //获取文件输入流
            InputStream inputStream = file.getInputStream();
            //读取数据
            EasyExcel.read(inputStream,ExcelSubject.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"添加课程失败");
        }
    }

    @Override
    public List<OneSubject> getAllSubject() {
        //1 获取所有的一级分类
        QueryWrapper<Subject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id",0);
        List<Subject> oneSubjectList = baseMapper.selectList(wrapperOne);
        //2 获取所有的二级分类
        QueryWrapper<Subject> wrapperTwo = new QueryWrapper<>();
        wrapperOne.ne("parent_id",0);
        List<Subject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //定义最终需要返回的对象集合
        List<OneSubject> finalSubjectList=new ArrayList<>();


        //3 封装一级分类
        //遍历所有的一级分类，将每个一级分类对象取出来，封装到要求的list集合里边
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //取出一级分类对象
            Subject subject = oneSubjectList.get(i);
            //创建封装的一级分类对象
            OneSubject oneSubject = new OneSubject();
            //将取出的一级分类对象里的字段复制到封装好的一级分类字段中
            BeanUtils.copyProperties(subject,oneSubject);
            //将封装好的对象放到需要返回对象的集合中
            finalSubjectList.add(oneSubject);

            //定义一个集合用于封装二级分类
            List<TwoSubject> twoFinalSubjectList=new ArrayList<>();
            //4 封装二级分类
            for (int j = 0; j < twoSubjectList.size() ; j++) {
                //获取每个二级分类对象
                Subject twoSubject = twoSubjectList.get(j);

                //判断当前二级分类对象的父亲是哪个一级分类
                //如果相等，将其封装属性赋给封装的二级分类
                if(twoSubject.getParentId().equals(oneSubject.getId())){
                    //定义封装的二级分类对象
                    TwoSubject tSubject=new TwoSubject();
                    BeanUtils.copyProperties(twoSubject,tSubject);
                    //将其加入到集合中
                    twoFinalSubjectList.add(tSubject);
                }
            }
            //最后，将这个二级分类集合加入到封装的一级分类中
            oneSubject.setChildren(twoFinalSubjectList);

        }

        return finalSubjectList;
    }
}
